package client;

import client.ParticleSystem.DustPuff;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.PhysicsTickListener;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.math.ColorRGBA;

/**
 *
 * @author Ben
 */
public class Projectile extends RigidBodyControl implements PhysicsCollisionListener, PhysicsTickListener
{
    public Projectile(){}
    
    public Projectile(CollisionShape shape, float mass)
    {
        super(shape,mass);
    }
    
    @Override
    public void setPhysicsSpace(PhysicsSpace space) {
        super.setPhysicsSpace(space);
        if (space != null) {
            space.addCollisionListener(this);
        }
    }
    
    public void collision(PhysicsCollisionEvent event)
    {
        if(space == null)
        {
            return;
        }
        if(event.getObjectA() == this || event.getObjectB() == this)
        {
            if(event.getObjectA() == this)
            {
                ControlDestroy des = new ControlDestroy(5000L);
                event.getNodeA().addControl(des);
                event.getNodeA().removeControl(this);
                DustPuff bop = new DustPuff("arrowpuff", event.getPositionWorldOnA(), ColorRGBA.Brown);
                bop.create();
                bop.start();
            }
            if(event.getObjectB() == this)
            {
                ControlDestroy des = new ControlDestroy(5000L);
                event.getNodeB().addControl(des);
                event.getNodeB().removeControl(this);
                DustPuff bop = new DustPuff("arrowpuff", event.getPositionWorldOnB(), ColorRGBA.Brown);
                bop.create();
                bop.start();
            }
            space.addTickListener(this);
            space.remove(this);
        }
    }
    
    public void physicsTick(PhysicsSpace space, float f)
    {
        space.removeCollisionListener(this);
        space.removeTickListener(this);
    }

    public void prePhysicsTick(PhysicsSpace space, float f)
    {
        space.removeCollisionListener(this);
    }
    
    @Override
    public void update(float tpf)
    {
        super.update(tpf);
    }
}
